//
//  NSDate+NSDate_Extension.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/27.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "NSDate+NSDate_Extension.h"
#import <objc/message.h>

@implementation NSDate (NSDate_Extension)

-(void)setSinaDateString:(NSString *)sinaDateString{
    
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    dt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    if ([self isThisYear:[NSDate sinaDate:self]]) {//是今年
        NSCalendar *currentCalendar = [NSCalendar currentCalendar];
        
        if ([currentCalendar isDateInToday:self]) {
            NSTimeInterval timeInterVal = ABS([self timeIntervalSinceNow]);
            if (timeInterVal < 60) {
                sinaDateString = [NSString stringWithFormat:@"刚刚"];
            } else if (timeInterVal < 3600) {
                int result = timeInterVal / 60;
//                return "\(Int(result))分钟前"
                sinaDateString = [NSString stringWithFormat:@"%zd分钟前",result];
            } else {
                int result = timeInterVal / 3600;
//                return "\(Int(result))小时前"
                sinaDateString = [NSString stringWithFormat:@"%zd小时前",result];
            }

        }else if ([currentCalendar isDateInYesterday:self]){
            dt.dateFormat = @"昨天 HH:mm";
        }else{
            //  其它
            dt.dateFormat = @"MM-dd HH:mm";
        }
    }else{//不是今年
        dt.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    
    
    sinaDateString = [dt stringFromDate:self];
    
    objc_setAssociatedObject(self, @"sinaDateString", sinaDateString, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)sinaDateString{// po self.sinaDateString
//error: property 'sinaDateString' not found on object of type 'NSDate *'
    NSString *sinaDateString = @"";
    
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    dt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    if ([self isThisYear:[NSDate sinaDate:self]]) {//是今年
        NSCalendar *currentCalendar = [NSCalendar currentCalendar];
        
        if ([currentCalendar isDateInToday:self]) {
            NSTimeInterval timeInterVal = ABS([self timeIntervalSinceNow]);
            if (timeInterVal < 60) {
                sinaDateString = [NSString stringWithFormat:@"刚刚"];
            } else if (timeInterVal < 3600) {
                int result = timeInterVal / 60;
                //                return "\(Int(result))分钟前"
                sinaDateString = [NSString stringWithFormat:@"%zd分钟前",result];
            } else {
                int result = timeInterVal / 3600;
                //                return "\(Int(result))小时前"
                sinaDateString = [NSString stringWithFormat:@"%zd小时前",result];
            }
            
        }else if ([currentCalendar isDateInYesterday:self]){
            dt.dateFormat = @"昨天 HH:mm";
        }else{
            //  其它
            dt.dateFormat = @"MM-dd HH:mm";
        }
    }else{//不是今年
        dt.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    
    
    sinaDateString = [dt stringFromDate:self];
    

    return sinaDateString;
}

+(instancetype)sinaDate:(NSString *)createAt{
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    
    dt.dateFormat = @"EEE MMM dd HH:mm:ss z yyyy";
    
    dt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    if ([createAt isKindOfClass:[NSDate class]]) {
        NSLog(@"inputString is of type NSDate");
    }
     //  把字符串转成时间对象
    NSDate *date = [dt dateFromString:createAt];
    return date;
}

-(BOOL)isThisYear:(NSDate *)createAtDate{
    
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    
    dt.dateFormat = @"yyyy";
    
    dt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    //  //  获取发微博时间的年份
    NSString *createAtYear = [dt stringFromDate:createAtDate];
    
    //  获取当前时间的年份
    NSString *currentDateYear = [dt stringFromDate:[NSDate date]];
    
    
    return [createAtYear isEqualToString:currentDateYear];
}

@end
